<?php
namespace App\Repos;

use App\Models\User;
use App\Models\Wxuser;

class WxUserRepo
{

    /**
     * @param $wechatUser
     * @return mixed
     */
    public static function loginOrRegister($wechatUser)
    {

        $wechatInfo = $wechatUser->original;
        //获取用户openid
        $openId     = array_get($wechatInfo,'openid');
        //查询用户注册已经注册
        $wxUser = User::where(['openid'=>$openId])->first();

        if(!$wxUser){
            try {
                User::create(array_only($wechatInfo,['openid','nickname','avatar']));

            } catch (\Exception $e) {
                return $wechatInfo;
            }
        }
        return $wechatInfo;
    }

}